/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gifer;

import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.io.*;
import java.net.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author victor
 */
public class prueba extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ServletOutputStream bufferSalida = response.getOutputStream();

     Frame ventanaMarco = new Frame(); 
     ventanaMarco.addNotify(); 
     Graphics contextoGrafico=null;

     try 
     {
      // Construct a matching-size off screen graphics context
          int w = 200;
          int h = 50;

          Image offscreen = ventanaMarco.createImage(w, h);
          contextoGrafico = offscreen.getGraphics();
     
          contextoGrafico.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 20));

          contextoGrafico.draw3DRect(0,0,w-1,h-1,true);

          Date ahora = new Date();
          SimpleDateFormat formateador = new SimpleDateFormat("dd-MM-yyyy HH:mm");

          contextoGrafico.drawString(formateador.format(ahora), 20, 30);

          // Encode the off screen graphics into a GIF and send it to the client
          response.setContentType("image/gif");
          //GifEncoder encoder = new GifEncoder(offscreen, bufferSalida);
          //encoder.encode();

          bufferSalida.flush();
          bufferSalida.close();
     }
     finally 
     {
          if (contextoGrafico != null) contextoGrafico.dispose();
     }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
